package com.zhitan.ems.app.stat.controller;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhitan.ems.app.stat.entity.StatisticsDayEntity;
import com.zhitan.ems.app.stat.service.StatisticsDayService;
import com.zhitan.ems.app.stat.vo.StatisticsDayVO;
import com.zhitan.ems.app.stat.wrapper.StatisticsDayWrapper;
import com.zhitan.ems.common.annotation.Log;
import com.zhitan.ems.common.core.controller.BaseController;
import com.zhitan.ems.common.core.domain.R;
import com.zhitan.ems.common.enums.BusinessType;
import com.zhitan.ems.common.utils.DateUtils;
import com.zhitan.ems.common.utils.StringUtils;
import com.zhitan.ems.common.utils.poi.ExcelUtil;
import com.zhitan.ems.framework.core.vo.VPage;
import com.zhitan.ems.framework.core.vo.VQueryKit;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
/**
 * 天统计数据表
 * @author zhitan
 * 
 * @date 2025-09-19 22:59:37
 */
@Api(tags = "天统计数据表", value = "天统计数据表")
@RestController
@RequestMapping("intra/stat/statisticsday")
public class StatisticsDayController extends BaseController{
    @Autowired
    private StatisticsDayService statisticsDayService;
    /**
     * 列表
     */
    @ApiOperation(value = "列表", response = VPage.class)
    @ApiImplicitParams({
		        @ApiImplicitParam(name = "name", value = "标题",  paramType = "query", required = false, dataType =  "String"),
		})
    @GetMapping("/browse")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<VPage<StatisticsDayVO>> browse(@RequestParam Map<String, Object> params){
        QueryWrapper<StatisticsDayEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), StatisticsDayEntity.class);
        queryWrapper.orderByDesc("id");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
        IPage<StatisticsDayEntity> pages = statisticsDayService.page(VQueryKit.getPage(params), queryWrapper);
        return R.data(StatisticsDayWrapper.build().toVPageVO(pages));
    }
    @GetMapping("/listStat")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
    public R<Map<String, Object>> listStat(@RequestParam Map<String, Object> params)throws ParseException{
         QueryWrapper<StatisticsDayEntity> queryWrapper=VQueryKit.getQueryWrapperByMap(params,Arrays.asList("name"), StatisticsDayEntity.class);
         queryWrapper.select("count(id) as total,sum(id) as all");
         queryWrapper.like(StringUtils.isNotNull(params.get("name")),"name", params.get("name"));
        if(params.containsKey("createDate")) {
            queryWrapper.between("create_at",
                    DateUtils.getStartOfDate(DateUtils.str_ToDate(params.get("createDate").toString())),
                    DateUtils.getEndOfDate(DateUtils.str_ToDate(params.get("createDate").toString()))
              );   
        }
        Map<String, Object>  stat=statisticsDayService.getMap(queryWrapper);
        return R.data(stat);
    }
    @ApiOperation("导出")
	@Log(title = "参数管理", businessType = BusinessType.EXPORT)
	@PreAuthorize("@ss.hasPermi('stat:statisticsday:list')")
	@PostMapping("/toExport")
	public void toExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {
		QueryWrapper<StatisticsDayEntity> queryWrapper = VQueryKit.getQueryWrapperByMap(params, Arrays.asList("name"),
				StatisticsDayEntity.class);
		queryWrapper.orderByDesc("id");
		queryWrapper.like(StringUtils.isNotNull(params.get("name")), "name", params.get("name"));
		 List<StatisticsDayEntity> statisticsDayList = statisticsDayService.list(queryWrapper);
		ExcelUtil<StatisticsDayEntity> util = new ExcelUtil<>(StatisticsDayEntity.class);
		util.exportExcel(response, statisticsDayList, "参数数据");
	}
   
    /**
     * 信息
     */
    @ApiOperation(value = "信息", response = StatisticsDayVO.class)
    @GetMapping("/detail/{id}")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:info')")
    public R<StatisticsDayVO> detail(@PathVariable("id") Long id){
		StatisticsDayEntity statisticsDay = statisticsDayService.getById(id);
	    return R.data(StatisticsDayWrapper.build().entityVO(statisticsDay));
    }
     /**
     * 保存&更新 
     */
    @Log(title = "天统计数据表", businessType = BusinessType.ADDANDEDIT)
    @ApiOperation(value = "保存&更新", response = Boolean.class)
    @PostMapping("/submit")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:save')")
    public R<StatisticsDayVO> submit(@Valid @RequestBody StatisticsDayVO statisticsDay){
    	 if(statisticsDay.getId()==null) {
           return add(statisticsDay);
         }else {
           return edit(statisticsDay);
        }
    }
    
    @Log(title = "天统计数据表", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增", response = Boolean.class)
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:save')")
    public R<StatisticsDayVO> add(@Valid @RequestBody StatisticsDayVO statisticsDay){
   		if(statisticsDay.getId()==null) {
            statisticsDay.setCreateAt(new Date());
            statisticsDay.setIsDelete(0);
            statisticsDay.setUpdateAt(new Date());
        } 
    	if(statisticsDayService.save(statisticsDay)) {
    		 return R.ok(statisticsDay);
    	 }
    	return R.error("数据错误");
    }
    
    @Log(title = "天统计数据表", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "更新", response = Boolean.class)
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:update')")
    public R<StatisticsDayVO> edit(@Valid @RequestBody StatisticsDayVO statisticsDay){
   		statisticsDay.setUpdateAt(new Date());
    	if(statisticsDayService.updateById(statisticsDay)) {
    		 return R.ok(statisticsDay);
    	 }
    	return R.error("数据错误");
    }
    /**
     * 删除
     */
    @ApiOperation(value = "删除", response = Boolean.class)
    @DeleteMapping("/delete")
    @PreAuthorize("@ss.hasPermi('stat:statisticsday:delete')")
    @Log(title = "天统计数据表", businessType = BusinessType.DELETE)
    public R<Boolean> delete(@RequestBody Long[] ids){
        return R.ok(statisticsDayService.removeByIds(Arrays.asList(ids)));
    }

}
